/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.data.HttpProxy');
(function () {
	
	/**
	 * An ASC specific implementation of the HttpProxy to process the JsonResponse structure
	 * to provide low level error checking
	 * @class HttpProxy
	 * @extends Ext.data.DataProxy
	 * @namespace net.autodata.data
	 */
	var HttpProxy = Ext.extend(Ext.data.DataProxy, {
	
		/**
		 * @property method
		 * @type String
		 */
		method: 'POST',
		
		/**
		 * @property url
		 * @type String
		 */
		url: undefined,
		
		/**
		 * @property timeout
		 * @type int
		 */
		timeout: undefined,
		
		/**
		 * @property handleAs
		 * @type String
		 */
		handleAs: 'json',
		
		constructor: function (cfg)
		{
			HttpProxy.superclass.constructor.call(this, cfg);
	        Ext.apply(this, cfg);
		},
		
		/**
	     * @cfg {Function} doRequest Abstract method that should be implemented in all subclasses.  Note: Should only be used by custom-proxy developers.
	     * (e.g.: {@link Ext.data.HttpProxy#doRequest HttpProxy.doRequest},
	     * {@link Ext.data.DirectProxy#doRequest DirectProxy.doRequest}).
	     */
		doRequest: function (action, rs, params, reader, callback, scope, options)
		{
			if (action !== 'read') {
				callback.call(scope || this, null, options, false);
			}
			
			
			this.executeRequest(action, rs, params, reader, callback, scope, options);			
		},
		
		/**
		 * @method processResponse
		 * @private
		 */
		processResponse: function (response, params, reader, callback, scope, options)
		{
			try {
                var result = reader.readRecords(response);
            } catch (e) {
                this.fireEvent("loadexception", this, params, response, e);
                callback.call(scope || this, null, options, false);
                return;
            }
            
            this.fireEvent("load", this, response, options);
            callback.call(scope, result, options, true);
		},
		
		/**
		 * @method executeRequest
		 */
		executeRequest: function (action, rs, params, reader, callback, scope, options)
		{
			if (this.fireEvent('beforeload', this, params) !== false) {
				
				//TODO based on the handleAs property use the matching ajax request class
				//hardcoded to JSON right now
				var ajax = new net.autodata.data.JsonRequest({
					method: this.method,
					url: this.url
				});
				
				ajax.request({
					params: params,
					
					success: this._onSuccess,
					failure: this._onFailure,
					scope: this,
					
					proxyArgs: {
						params: params,
						reader: reader,
						callback: callback,
						scope: scope,
						options: options
					}
				});
			}
		},
		
		/**
		 * @method _onSuccess
		 * @private
		 */
		_onSuccess: function (response, ioArgs)
		{
			var proxyArgs = ioArgs.options.proxyArgs;			
			
			return this.processResponse(response, proxyArgs.params, proxyArgs.reader, 
				proxyArgs.callback, proxyArgs.scope, proxyArgs.options);			
		},
				
		/**
		 * @method _onFailure
		 * @private
		 */
		_onFailure: function (response, ioArgs)
		{
			var proxyArgs = ioArgs.options.proxyArgs;			
			var message = response.message || 'Unexpected data error';
			
			this.fireEvent('loadexception', this, proxyArgs.options, ioArgs.xhr, message);
			proxyArgs.callback.call(proxyArgs.scope, null, proxyArgs.options, false);
		}
	
	});
	
	net.autodata.data.HttpProxy = HttpProxy;		
})();